// import { createRouter, createWebHistory } from 'vue-router';
// import AuthLogin from '@/views/AuthLogin.vue';
// import AdminDashboard from '@/views/AdminDashboard.vue';
// import StudentDashboard from '@/views/StudentDashboard.vue';
// import ClassLeaderDashboard from '@/views/ClassLeaderDashboard.vue';

// const router = createRouter({
//   history: createWebHistory(),
//   routes: [
//     { path: '/', redirect: '/login' }, // 重定向到登录页
//     { path: '/login', component: AuthLogin, name: 'AuthLogin' }, // 登录页
//     {
//       path: '/admin/dashboard', // 宿管路由
//       component: AdminDashboard,
//       name: 'AdminDashboard',
//       meta: { role: 'admin' } // 路由元信息标记角色
//     },
//     {
//       path: '/student/dashboard', // 学生路由
//       component: StudentDashboard,
//       name: 'StudentDashboard',
//       meta: { role: 'student' }
//     },
//     {
//       path: '/classLeader/dashboard', // 班干部路由
//       component: ClassLeaderDashboard,
//       name: 'ClassLeaderDashboard',
//       meta: { role: 'classLeader' }
//     }
//   ]
// });

// // 路由守卫（验证角色权限，可选）
// router.beforeEach((to, from, next) => {
//   // 从sessionStorage获取用户信息
//   const user = sessionStorage.getItem('user');
  
//   // 未登录时重定向到登录页
//   if (to.meta.requiresAuth && !user) {
//     next('/login');
//   } else {
//     // 验证角色权限（示例：确保用户角色与路由匹配）
//     if (to.meta.role && user) {
//       const currentUser = JSON.parse(user);
//       if (currentUser.role !== to.meta.role) {
//         next('/login'); // 角色不匹配时禁止访问
//       }
//     }
//     next();
//   }
// });

// export default router;










import { createRouter, createWebHistory } from 'vue-router';

// 定义路由配置
const routes = [
  // 公共路由
  { path: '/', redirect: '/login' },
  { path: '/login', component: () => import('@/views/auth/AuthLogin.vue') },
  // { path: '/', redirect: '/ai' },
  // { path: '/ai', component: () => import('@/views/auth/AI.vue') },
  { path: '/register', component: () => import('@/views/auth/AuthRegister.vue') },
  
  // 学生路由
  {
    path: '/student',
    component: () => import('@/views/student/StudentLayout.vue'),
    meta: { requiresAuth: true, role: 'student' },
    children: [
      { path: 'dashboard', component: () => import('@/views/student/StudentDashboard.vue') },
      { path: 'repair', component: () => import('@/views/student/RepairForm.vue') },
      { path: 'garbage', component: () => import('@/views/student/GarbagePoints.vue') },
      //   { path: 'timetable', component: () => import('@/views/student/Timetable.vue') },
      { path: 'notices', component: () => import('@/views/student/NoticesAccept.vue') },
      { path: 'classnotices', component: () => import('@/views/student/ClassNoticesAccept.vue') },
      {
        path: '/detail/:id', // 动态路由参数
        name: 'NoticeDetail',
        component: () => import('@/views/student/NoticeDetail.vue'),
      },
      {
        path: '/classDetail/:id', // 动态路由参数
        name: 'ClassNoticeDetail',
        component: () => import('@/views/student/ClassNoticeDetail.vue'),
      },
      { path: 'posts', component: () => import('@/views/student/ClassPosts.vue') },
      { path: 'table', component: () => import('@/views/student/StudentCourseTable.vue') },


      //   { path: 'posts', component: () => import('@/views/student/ClassPosts.vue') },
        { path: 'evaluation', component: () => import('@/views/student/StudentEvaluation.vue') },
        { path: 'weather', component: () => import('@/views/student/WeatherView.vue') },
        // { path: 'aichat', component: () => import('@/views/student/AiChat.vue') }
          { path: '/ai', component: () => import('@/views/student/AI.vue') }
    
    ]
  },
  
  // 班干部路由 (继承学生权限，新增管理功能)
  {
    path: '/classLeader',
    component: () => import('@/views/classLeader/classLeaderLayout.vue'),
    meta: { requiresAuth: true, role: 'classLeader' },
    children: [
      { path: 'dashboard', component: () => import('@/views/classLeader/classLeaderDashboard.vue') },
      { path: 'uploadTimetable', component: () => import('@/views/classLeader/TimetableUpload.vue') },
      { path: 'notice', component: () => import('@/views/classLeader/NoticePublish.vue') },
      // 复用学生模块
      { path: 'repair', component: () => import('@/views/student/RepairForm.vue') },
      { path: 'garbage', component: () => import('@/views/student/GarbagePoints.vue') },
      { path: 'evaluation', component: () => import('@/views/classLeader/ClassEvaluations.vue') },
      { path: 'weather', component: () => import('@/views/classLeader/WeatherView.vue') },
        { path: '/ai', component: () => import('@/views/classLeader/AI.vue') }
     
    //   { path: 'notices', component: () => import('@/views/student/Notices.vue') },
      
    //   { path: 'evaluation', component: () => import('@/views/student/Evaluation.vue') }
    ]
  },
  
  // 宿管路由
  {
    path: '/admin',
    component: () => import('@/views/admin/AdminLayout.vue'),
    meta: { requiresAuth: true, role: 'admin' },
    children: [
      { path: 'dashboard', component: () => import('@/views/admin/AdminDashboard.vue') },
      { path: 'repairs', component: () => import('@/views/admin/RepairList.vue') },
    //   { path: 'handleRepair/:id', component: () => import('@/views/admin/RepairHandle.vue') },
      { path: 'publishNotice', component: () => import('@/views/admin/NoticePublish.vue') },
        { path: 'evaluation', component: () => import('@/views/admin/AdminEvaluation.vue') },
        { path: 'weather', component: () => import('@/views/classLeader/WeatherView.vue') },
          { path: '/ai', component: () => import('@/views/classLeader/AI.vue') }
    ]
  }
];

// 创建路由实例（解决 createRouter 和 createWebHistory 未使用问题）
const router = createRouter({
  history: createWebHistory(), // 使用历史模式
  routes // 传入路由配置
});

// // 路由守卫 - 权限控制（解决 router 未定义问题）
// router.beforeEach((to, from, next) => {
//   const user = JSON.parse(sessionStorage.getItem('user') || 'null');
  
//   if (to.meta.requiresAuth && !user) {
//     next('/login');
//   } else if (to.meta.role && user && user.role !== to.meta.role) {
//     next(`/${user.role}/dashboard`); // 重定向到用户角色首页
//   } else {
//     next();
//   }
// });

export default router;